package cn.xumaoyang.pay.controller;
import cn.xumaoyang.order.domain.AdoptOrder;
import cn.xumaoyang.order.service.IAdoptOrderService;
import cn.xumaoyang.pay.constants.AlipayConfig;
import cn.xumaoyang.pay.constants.PayConstants;
import cn.xumaoyang.pay.domain.AlipayInfo;
import cn.xumaoyang.pay.domain.PayBill;
import cn.xumaoyang.pay.service.IAlipayInfoService;
import cn.xumaoyang.pay.service.IPayBillService;
import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * 支付宝支付接口
 */

@RestController
public class AlipayController {

    @Autowired
    private IPayBillService payBillService;

    @Autowired
    private IAlipayInfoService alipayInfoService;

    @Autowired
    private IAdoptOrderService adoptOrderService;

    //用到了内网穿透带上这个接口。
    @PostMapping("/zhifu")
    public void zhifu(HttpServletRequest request){
        try {
            //获取支付宝POST过来反馈信息
            Map<String,String> params = new HashMap<String,String>();
            Map<String,String[]> requestParams = request.getParameterMap();
            for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext();) {
                String name = (String) iter.next();
                String[] values = (String[]) requestParams.get(name);
                String valueStr = "";
                for (int i = 0; i < values.length; i++) {
                    valueStr = (i == values.length - 1) ? valueStr + values[i]
                            : valueStr + values[i] + ",";
                }
                params.put(name, valueStr);
            }
            String unionPaySn = params.get("out_trade_no");//统一支付标识

            PayBill payBill = payBillService.loadByUnionPaySn(unionPaySn);
            AlipayInfo info = alipayInfoService.getByShopId(payBill.getShop_id());

            //验证签名
            boolean signVerified = AlipaySignature.rsaCheckV1(params,
                    info.getAlipay_public_key(),//阿里的公钥。
                    AlipayConfig.charset,
                    AlipayConfig.sign_type); //调用SDK验证签名
            if(signVerified) {//验证成功
                //商户订单号
                String out_trade_no = request.getParameter("out_trade_no");
                //支付宝交易号
                String trade_no = request.getParameter("trade_no");
                //交易状态
                String trade_status = request.getParameter("trade_status");

                if(trade_status.equals("TRADE_FINISHED")){

                }else if (trade_status.equals("TRADE_SUCCESS")){
                    //2.查询支付单，修改支付单状态  记录支付流水
                    //3.反查询业务订单，把业务订单状态改了
                    payBill.setState(1);//设置状态。
                    payBill.setUpdateTime(new Date());//设置时间。
                    payBillService.update(payBill);//修改。
                    //区分业务类型。
                    if(PayConstants.BUSINESS_YPE_ADOPT_ORDER.equals(payBill.getBusinessType())){//领养订单的支付单
                        //查询
                        AdoptOrder adoptOrder = adoptOrderService.loadById(payBill.getBusinessKey());
                        //修改状态。
                        adoptOrder.setState(1);
                        //修改数据
                        adoptOrderService.update(adoptOrder);
                    }
                }

            }else {


            }
            System.out.println("通知到我了！");
        } catch (Exception e) {
            e.printStackTrace();
        }
        }
    }

